<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="工资单编号" prop="salaryNo">
        <el-input v-model="formData.salaryNo" placeholder="请输入工资单编号" />
      </el-form-item>
<!--      <el-form-item label="员工ID" prop="userId">-->
<!--        <el-input v-model="formData.userId" placeholder="请输入员工ID" />-->
<!--      </el-form-item>-->
      <el-form-item label="员工姓名" prop="userName">
        <el-input v-model="formData.userName" placeholder="请输入员工姓名" />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入部门ID" />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="formData.deptName" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="工资月份" prop="yearMonthValue">
        <el-input v-model="formData.yearMonthValue" placeholder="请输入工资月份" />
      </el-form-item>
      <el-form-item label="基本工资" prop="baseSalary">
        <el-input v-model="formData.baseSalary" placeholder="请输入基本工资" />
      </el-form-item>
      <el-form-item label="岗位工资" prop="positionSalary">
        <el-input v-model="formData.positionSalary" placeholder="请输入岗位工资" />
      </el-form-item>
      <el-form-item label="工龄工资" prop="senioritySalary">
        <el-input v-model="formData.senioritySalary" placeholder="请输入工龄工资" />
      </el-form-item>
      <el-form-item label="绩效工资" prop="performanceSalary">
        <el-input v-model="formData.performanceSalary" placeholder="请输入绩效工资" />
      </el-form-item>
      <el-form-item label="奖金" prop="bonus">
        <el-input v-model="formData.bonus" placeholder="请输入奖金" />
      </el-form-item>
      <el-form-item label="餐补" prop="mealAllowance">
        <el-input v-model="formData.mealAllowance" placeholder="请输入餐补" />
      </el-form-item>
      <el-form-item label="交通补贴" prop="transportAllowance">
        <el-input v-model="formData.transportAllowance" placeholder="请输入交通补贴" />
      </el-form-item>
      <el-form-item label="通讯补贴" prop="communicationAllowance">
        <el-input v-model="formData.communicationAllowance" placeholder="请输入通讯补贴" />
      </el-form-item>
      <el-form-item label="住房补贴" prop="housingAllowance">
        <el-input v-model="formData.housingAllowance" placeholder="请输入住房补贴" />
      </el-form-item>
      <el-form-item label="其他补贴" prop="otherAllowance">
        <el-input v-model="formData.otherAllowance" placeholder="请输入其他补贴" />
      </el-form-item>
      <el-form-item label="考勤扣款" prop="attendanceDeduction">
        <el-input v-model="formData.attendanceDeduction" placeholder="请输入考勤扣款" />
      </el-form-item>
      <el-form-item label="其他扣款" prop="otherDeduction">
        <el-input v-model="formData.otherDeduction" placeholder="请输入其他扣款" />
      </el-form-item>
      <el-form-item label="社保个人部分" prop="socialInsurance">
        <el-input v-model="formData.socialInsurance" placeholder="请输入社保个人部分" />
      </el-form-item>
      <el-form-item label="公积金个人部分" prop="housingFund">
        <el-input v-model="formData.housingFund" placeholder="请输入公积金个人部分" />
      </el-form-item>
      <el-form-item label="应纳税所得额" prop="taxableIncome">
        <el-input v-model="formData.taxableIncome" placeholder="请输入应纳税所得额" />
      </el-form-item>
      <el-form-item label="个人所得税" prop="personalTax">
        <el-input v-model="formData.personalTax" placeholder="请输入个人所得税" />
      </el-form-item>
      <el-form-item label="实发工资" prop="netSalary">
        <el-input v-model="formData.netSalary" placeholder="请输入实发工资" />
      </el-form-item>
      <el-form-item label="状态" prop="salaryStatus">
        <el-radio-group v-model="formData.salaryStatus">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="发放日期" prop="paymentDate">
        <el-date-picker
          v-model="formData.paymentDate"
          type="date"
          value-format="x"
          placeholder="选择发放日期"
        />
      </el-form-item>
      <el-form-item label="发放方式" prop="paymentMethod">
        <el-input v-model="formData.paymentMethod" placeholder="请输入发放方式" />
      </el-form-item>
      <el-form-item label="银行账号" prop="bankAccount">
        <el-input v-model="formData.bankAccount" placeholder="请输入银行账号" />
      </el-form-item>
      <el-form-item label="开户银行" prop="bankName">
        <el-input v-model="formData.bankName" placeholder="请输入开户银行" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="审批结果" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { HrSalaryMonthlyApi, HrSalaryMonthlyVO } from '@/api/bpm/hrsalarymonthly'

/** 月度工资明细 表单 */
defineOptions({ name: 'HrSalaryMonthlyForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  salaryNo: undefined,
  userId: undefined,
  userName: undefined,
  deptId: undefined,
  deptName: undefined,
  yearMonthValue: undefined,
  baseSalary: undefined,
  positionSalary: undefined,
  senioritySalary: undefined,
  performanceSalary: undefined,
  bonus: undefined,
  mealAllowance: undefined,
  transportAllowance: undefined,
  communicationAllowance: undefined,
  housingAllowance: undefined,
  otherAllowance: undefined,
  attendanceDeduction: undefined,
  otherDeduction: undefined,
  socialInsurance: undefined,
  housingFund: undefined,
  taxableIncome: undefined,
  personalTax: undefined,
  netSalary: undefined,
  salaryStatus: undefined,
  paymentDate: undefined,
  paymentMethod: undefined,
  bankAccount: undefined,
  bankName: undefined,
  remark: undefined,
  status: undefined,
})
const formRules = reactive({
  salaryNo: [{ required: true, message: '工资单编号不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '员工ID不能为空', trigger: 'blur' }],
  userName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
  deptId: [{ required: true, message: '部门ID不能为空', trigger: 'blur' }],
  deptName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
  yearMonthValue: [{ required: true, message: '工资月份不能为空', trigger: 'blur' }],
  baseSalary: [{ required: true, message: '基本工资不能为空', trigger: 'blur' }],
  positionSalary: [{ required: true, message: '岗位工资不能为空', trigger: 'blur' }],
  senioritySalary: [{ required: true, message: '工龄工资不能为空', trigger: 'blur' }],
  performanceSalary: [{ required: true, message: '绩效工资不能为空', trigger: 'blur' }],
  bonus: [{ required: true, message: '奖金不能为空', trigger: 'blur' }],
  mealAllowance: [{ required: true, message: '餐补不能为空', trigger: 'blur' }],
  transportAllowance: [{ required: true, message: '交通补贴不能为空', trigger: 'blur' }],
  communicationAllowance: [{ required: true, message: '通讯补贴不能为空', trigger: 'blur' }],
  housingAllowance: [{ required: true, message: '住房补贴不能为空', trigger: 'blur' }],
  otherAllowance: [{ required: true, message: '其他补贴不能为空', trigger: 'blur' }],
  attendanceDeduction: [{ required: true, message: '考勤扣款不能为空', trigger: 'blur' }],
  otherDeduction: [{ required: true, message: '其他扣款不能为空', trigger: 'blur' }],
  socialInsurance: [{ required: true, message: '社保个人部分不能为空', trigger: 'blur' }],
  housingFund: [{ required: true, message: '公积金个人部分不能为空', trigger: 'blur' }],
  taxableIncome: [{ required: true, message: '应纳税所得额不能为空', trigger: 'blur' }],
  personalTax: [{ required: true, message: '个人所得税不能为空', trigger: 'blur' }],
  netSalary: [{ required: true, message: '实发工资不能为空', trigger: 'blur' }],
  salaryStatus: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '审批结果不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await HrSalaryMonthlyApi.getHrSalaryMonthly(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as HrSalaryMonthlyVO
    if (formType.value === 'create') {
      await HrSalaryMonthlyApi.createHrSalaryMonthly(data)
      message.success(t('common.createSuccess'))
    } else {
      await HrSalaryMonthlyApi.updateHrSalaryMonthly(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    salaryNo: undefined,
    userId: undefined,
    userName: undefined,
    deptId: undefined,
    deptName: undefined,
    yearMonthValue: undefined,
    baseSalary: undefined,
    positionSalary: undefined,
    senioritySalary: undefined,
    performanceSalary: undefined,
    bonus: undefined,
    mealAllowance: undefined,
    transportAllowance: undefined,
    communicationAllowance: undefined,
    housingAllowance: undefined,
    otherAllowance: undefined,
    attendanceDeduction: undefined,
    otherDeduction: undefined,
    socialInsurance: undefined,
    housingFund: undefined,
    taxableIncome: undefined,
    personalTax: undefined,
    netSalary: undefined,
    salaryStatus: undefined,
    paymentDate: undefined,
    paymentMethod: undefined,
    bankAccount: undefined,
    bankName: undefined,
    remark: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>
